package featuresWindows;

import helpFeaturesWindows.HelpSlotWindow;
import j3d.J3D;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import CAD.MainFrame;

import operationSolids.CSGSolid;
import operationSolids.CompoundSolid;

import features.Block;
import features.Feature;
import features.PlanarFace;

import visualCAD.PlanarFaceWindowView;
import visualCAD.SlotWindow;
import visualCAD.holeWindowView;
public class CreatePlanarFace extends PlanarFaceWindowView implements ActionListener{

	private J3D j3d = null;
	private PlanarFacePanel planarPanel;
	Double depth = 0.0;
	public CreatePlanarFace(J3D j3d, Block block)
	{
		super();
		setVisible(true);
		pack();
		planarFacePanel.add(planarPanel=new PlanarFacePanel(depth),BorderLayout.CENTER);
		this.j3d = j3d;
		this.init();
		
	}
	public J3D getJ3d() {
		return j3d;
	}
	public void setJ3d(J3D j3d) {
		this.j3d = j3d;
	}
	
	
	public double getDepth() {
		return depth;
	}
	public void setDepth(double depth) {
		this.depth = depth;
	}
	public void init()
	{
		depthSpinner.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				redrawDynamicBoxes();
			}
		});
		
		this.submitButton.addActionListener(this);
		this.cancelButton.addActionListener(this);
		this.setResizable(false);
	}
	private void redrawDynamicBoxes(){
		try{
			depth=(Double)depthSpinner.getValue();
			}
			catch (Exception e1) {
				JOptionPane.showMessageDialog(null, "error in entering data");
			}
			planarFacePanel.removeAll();
			planarFacePanel.add(planarPanel=new PlanarFacePanel(depth),BorderLayout.CENTER);
			planarFacePanel.repaint();
			planarFacePanel.revalidate();
	}
	public void actionPerformed(ActionEvent evt) {
		Object origin = evt.getSource();
		
		
		if (origin.equals(this.submitButton))
		{
			this.ok();
		}
		else if (origin.equals(this.cancelButton))
		{
			this.cancel();
		}
		else if (origin.equals(this.helpButton))
		{
			new HelpSlotWindow();
		}
	}
	private void cancel()
	{
		this.dispose();
	}
	private void ok() 
	{
		PlanarFace st = new PlanarFace(Feature.PLANARFACE,
				null,0,0,0,
				((Double)depth).floatValue(),
				this.nameTextField.getText(),
				MainFrame.Face );
		
		this.j3d.removeSolid();
		this.j3d.addSolid(MainFrame.block.block);
		this.dispose();
		}
	}
	

class PlanarFacePanel extends JComponent{
	
	Shape smallBox;
	Shape bigBox;
	Shape axisX, axisY;
	double faceX = 0;
	double faceY = 0;
	double faceZ = 0;
	double multiplier= 0;
	
	
	private void findXandY() {
		switch(MainFrame.Face){
		case MainFrame.TOPFACE:
			faceX = MainFrame.block.width;
			faceY = MainFrame.block.length;
			faceZ = MainFrame.block.height;
			break;
		case MainFrame.LOWERFACE:
			faceX = MainFrame.block.width;
			faceY = MainFrame.block.length;
			faceZ = MainFrame.block.height;
			break;
		case MainFrame.FRONTFACE:
			faceX = MainFrame.block.width;
			faceY = MainFrame.block.height;
			faceZ = MainFrame.block.length;
			break;
		case MainFrame.BACKFACE:
			faceX = MainFrame.block.width;
			faceY = MainFrame.block.height;
			faceZ = MainFrame.block.length;
			break;
		case MainFrame.EASTFACE:
			faceX = MainFrame.block.length;
			faceY = MainFrame.block.height;
			faceZ = MainFrame.block.width;
			break;
		case MainFrame.WESTFACE:
			faceX = MainFrame.block.length;
			faceY = MainFrame.block.height;
			faceZ = MainFrame.block.width;
			break;
		}
	}
	private double calculateMultiplier() {
		double multiplier = 195/faceY;
		if(faceX*multiplier > 310){
			multiplier = 310/faceX;
		}
		if(faceZ*multiplier > 180){
			multiplier = 180/faceZ;
		}
		return multiplier;
	}
	
	public PlanarFacePanel(double depth) {
		super();
		findXandY();
		multiplier = calculateMultiplier();
		smallBox=createBox(depth);
		bigBox=createRectangle();
		axisX = verticalLine();
		axisY = horizontalLine();
	}
	protected Shape createBox(double depth) {
		Shape rectangle = new Rectangle2D.Double(10,20,faceX*multiplier,depth*multiplier);
		return rectangle;
	}
	protected Shape createRectangle() {
		Shape rectangle = new Rectangle2D.Double(10,20,faceX*multiplier,faceZ*multiplier);
		return rectangle;
	}
	protected Shape verticalLine() {
		
		Shape axisY = new Line2D.Double(10, 20, 10, 50);
		return axisY;
	}
	protected Shape horizontalLine(){
		Shape axisX = new Line2D.Double(10, 20, 50, 20);
		return axisX;
	}
	
	
	public void paint(Graphics g) {
		//System.out.println("translateY = " + translateY);
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		float dash1[] = {2.5f, 2.5f};
		
		g2.setPaint(new Color(211, 211, 211));
		g2.fill(bigBox);
		g2.setPaint(new Color(135, 206, 250));
		g2.fill(smallBox);
		g2.setPaint(Color.BLUE);
		g2.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, 
				BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f));
		g2.draw(smallBox);
		g2.setPaint(Color.BLACK);
		g2.setStroke(new BasicStroke());
		g2.draw(bigBox);
		g2.setPaint(Color.RED);
		g2.draw(axisX);
		g2.draw(axisY);
		g2.drawString("X", 50 ,20);
		g2.drawString("-Z", 0, 50);
	}
}
